package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

//        for (DishFlavor flavor : flavors) {
//            flavor.setDishId(dishId);
//        }
        flavors = flavors.stream().map(new Function<DishFlavor, DishFlavor>() {
            @Override
            public DishFlavor apply(DishFlavor dishFlavor) {
                dishFlavor.setDishId(dishId);

                return dishFlavor;
            }
        }).collect(Collectors.toList());

//        flavors = flavors.stream().map((item) -> {
//            item.setDishId(dishId);
//            return item;
//        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public DishDto getByIdWithFlavor(Long id) {

        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        //把获取的菜品对象复制到dishDto对象中
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;

    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {

        //更新dish表基本信息
        this.updateById(dishDto);//dishService

        //清理当前菜品对应口味数据---dish_flavor表的delete操作

        //目前存在的bug是口味的的删除并没有生效!//或者只是菜品的id没有删掉?
        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(null != dishDto.getId(), DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

//        for (DishFlavor flavor : flavors) {
//            //把前端传来的菜品对象的id(菜品自身的id)找到传递给口味对象中的菜品id属性(dishId)
//            flavor.setDishId(dishDto.getId());
//        }
        if (flavors != null) {
            List<DishFlavor> dishFlavors = flavors.stream().map(dishFlavor -> {
                dishFlavor.setDishId(dishDto.getId());
                dishFlavor.setId(null);
                return dishFlavor;
            }).collect(Collectors.toList());

            dishFlavorService.saveBatch(dishFlavors);
        }


    }

    //删除菜品和对应的口味
    @Override
    public void removeWithFlavor(List<Long> ids) {
        //先把菜品删除
        this.removeByIds(ids);

        //获取菜品下的口味然后删除
        //获取联系的方法是菜品口味的菜品编号dishId是菜品的编号Id
        QueryWrapper<DishFlavor> dishFlavorQueryWrapper = new QueryWrapper<>();
        dishFlavorQueryWrapper.lambda().in(null != ids, DishFlavor::getDishId, ids);
        dishFlavorService.remove(dishFlavorQueryWrapper);

    }


}
